const cloud = require('wx-server-sdk')
cloud.init({
    env: cloud.DYNAMIC_CURRENT_ENV
})
const db = cloud.database()

exports.main = async (event, context) => {
    try {
        const { limit = 20 } = event

        // 随机获取指定数量的模板
        const res = await db.collection('resume_templates')
            .aggregate()
            .sample({
                size: limit
            })
            .end()

        return {
            code: 0,
            message: 'success',
            data: {
                templates: res.list,
                total: res.list.length
            }
        }
    } catch (err) {
        console.error('获取随机模板失败:', err)
        return {
            code: -1,
            message: err.message || '获取随机模板失败',
            data: null
        }
    }
}